package com.chaosj.webmagic.convert;

import com.chaosj.webmagic.dto.SearchResponseDto;
import com.chaosj.webmagic.dto.SearchEnums;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Selectable;

/**
 * @className: AuthorConvertHandler
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/11
 **/
public abstract class AuthorBookConvertHandler<T extends SearchResponseDto> extends AbstractConvertPage<T> {
    @Override
    public void convert(Page page) {
        SearchResponseDto author = createOrGet(page);
        Selectable selectable = page.getHtml().xpath("//div[@id='leftZhankai']//div[@class='cont']");
        selectable.nodes().forEach(selected->{
            // 头像
            String img = selected.xpath("//div[@class='divimg']//img/@src").get();
            // 姓名
            String name = selected.xpath("//div/p").nodes().get(0).xpath("//b/text()").get();
            // 链接
            String target = holdTarget(selected.xpath("//a/@href").get());
            // 介绍
            String content = selected.xpath("//div/p").nodes().get(1).xpath("/p/text()").get();
            author.addObject(name, img, target, content);
        });
    }

    @Override
    public boolean support(SearchEnums instance) {
        return SearchEnums.ZUO_ZHE.equals(instance);
    }
}
